#include"StdAfx.h"

pgCamera::pgCamera(pgMat44 worldTransform, pgVec3 lookAt, float fNearPlane, 
				   float fFarPlane, float fAspectRatio, float fFov)
{
	m_worldTransform = worldTransform;
	m_lookAt = lookAt;
	m_up = m_worldTransform.GetUpAxis();
	m_pos = m_worldTransform.GetTranslation();
	m_fNearPlane = fNearPlane;
	m_fFarPlane = fFarPlane;
	m_fAspectRatio = fAspectRatio;
	m_fFov = fFov;

	m_bCreateProjection = true;
	m_bCreateView = true;
}

pgCamera::~pgCamera()
{
}

void pgCamera::SetLookAt(pgVec3 at)
{
	m_bCreateView = true;
	m_lookAt = at;
}
pgVec3 pgCamera::GetLookAt()
{
	return m_lookAt;
}

void pgCamera::SetFov(float fFov)
{
	if (fFov)
	{
		m_fFov = fFov;
		m_bCreateProjection = true;
	}
}
float pgCamera::GetFov()
{
	return m_fFov;
}

void pgCamera::SetWorldTransformMatrix(pgMat44 world)
{
	m_worldTransform = world;

	m_pos = m_worldTransform.GetTranslation();

	m_up =  m_worldTransform.GetUpAxis();


	m_lookAt = m_pos + m_worldTransform.GetFwdAxis().Normalize();

	m_bCreateView = true;
}
pgMat44 pgCamera::GetWorldTransformMatrix()
{
	return m_worldTransform;
}

pgMat44 pgCamera::GetViewMatrix()
{
	if (m_bCreateView)
	{
		m_viewMatrix = XMMatrixLookAtLH(m_pos, m_lookAt, m_up);
		m_bCreateView = false;
	}

	return m_viewMatrix;
}
pgMat44 pgCamera::GetProjectionMatrix()
{
	if (m_bCreateProjection)
	{
		m_projectionMatrix = XMMatrixPerspectiveFovLH(m_fFov, m_fAspectRatio, m_fNearPlane, m_fFarPlane);
		m_bCreateProjection = false;
	}

	return m_projectionMatrix;
}

void pgCamera::SetAspectRatio(float fAspectRatio)
{
	if (fAspectRatio)
	{
		m_fAspectRatio = fAspectRatio;
		m_bCreateProjection = true;
	}
}
float pgCamera::GetAspectRatio()
{
	return m_fAspectRatio;
}

void pgCamera::SetFarPlane(float fFarPlane)
{
	m_fFarPlane = fFarPlane;
	m_bCreateProjection = true;
}
float pgCamera::GetFarPlane()
{
	return m_fFarPlane;
}

void pgCamera::SetNearPlane(float fNearPlane)
{
	m_fNearPlane = fNearPlane;
	m_bCreateProjection = true;
}
float pgCamera::GetNearPlane()
{
	return m_fNearPlane;
}

pgVec3 pgCamera::GetPosition()
{
	return m_pos;
}

pgVec3 pgCamera::GetUpVector()
{
	return m_up;
}